home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / gg243730.zip / SWAPSIZE.C < prev    next >
Text File  |  1993-03-06  |  15KB  |  313 lines

  1. /**********************************************************/
  2. /**********************************************************/
  3. /***                                                    ***/
  4. /***  Program name: SWAPSIZE.EXE                        ***/
  5. /***                                                    ***/
  6. /***  Created     : February, 1992                      ***/
  7. /***                                                    ***/
  8. /***  Revised     :                                     ***/
  9. /***                                                    ***/
  10. /***  Author      : Darryl Frost                        ***/
  11. /***                                                    ***/
  12. /***  Purpose     : To be used with the Lab Session     ***/
  13. /***                Examples given in Appendix C of the ***/
  14. /***                OS/2 Version 2.0 Volume 1: Control  ***/
  15. /***                Program Document no GG24-3730.      ***/
  16. /***                This program interrogates and       ***/
  17. /***                displays the size of the            ***/
  18. /***                SWAPPER.DAT at regular intervals    ***/
  19. /***                in a PM window. The interval is     ***/
  20. /***                initially set to 10 seconds. This   ***/
  21. /***                may be changed to 30 seconds or     ***/
  22. /***                60 seconds by selecting the         ***/
  23. /***                Interval action bar.                ***/
  24. /***                                                    ***/
  25. /***  Parameter   : If the path of SWAPPER.DAT is other ***/
  26. /***                than C:\OS2\SYSTEM, the full path   ***/
  27. /***                and file name of the swapper file   ***/
  28. /***                must be the first parameter passed  ***/
  29. /***                to the progam when it is started.   ***/
  30. /***                                                    ***/
  31. /**********************************************************/
  32. /**********************************************************/
  33. #define INCL_WIN
  34. #define INCL_GPI
  35.  
  36. #include <os2.h>                        /* PM header file               */
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include "swapsize.h"                   /* Resource symbolic identifiers*/
  40.  
  41. #define STRINGLENGTH 20                 /* Length of string             */
  42.  
  43. /************************************************************************/
  44. /* Function prototypes                                                  */
  45. /************************************************************************/
  46. INT main(int argc, char *argv[], char *envp[] );
  47. VOID AbortSS(HWND hwndFrame,HWND hwndClient);
  48. MRESULT EXPENTRY SSWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  49. ULONG GetSwapperSize (void);
  50.                                         /* Define parameters by type    */
  51. HAB  hab;                               /* PM anchor block handle       */
  52. HWND hwndClient=0L;                     /* Client area window handle    */
  53. HWND hwndFrame=0L;                      /* Frame window handle          */
  54. CHAR *szString;                         /* procedure.                   */
  55. PSZ  pszErrMsg;
  56. char szFname[64] = "C:\\OS2\\SYSTEM\\SWAPPER.DAT";
  57. ULONG swapsize;
  58. ULONG oldswapsize;
  59. ULONG timerinterval = 10 * 1000;
  60. ULONG idTimer = 1;
  61.  
  62. /******************************************************************************/
  63. /*                                                                            */
  64. /* MAIN initializes the process for OS/2 PM services, and processes the       */
  65. /* application message queue until a WM_QUIT message is received. It          */
  66. /* then destroys all OS/2 PM resources and terminates.                        */
  67. /*                                                                            */
  68. /******************************************************************************/
  69. INT main (int argc, char *argv[], char *envp[])
  70. {
  71.   HMQ  hmq;                             /* Message queue handle         */
  72.   QMSG qmsg;                            /* Message from message queue   */
  73.   ULONG flCreate;                       /* Window creation control flags*/
  74.  
  75.   if ( argc >= 2 ) {
  76.      strcpy( szFname, argv[1] );
  77.   }
  78.  
  79.   if ((hab = WinInitialize(0)) == 0L) /* Initialize PM     */
  80.      AbortSS(hwndFrame, hwndClient); /* Terminate the application    */
  81.  
  82.   if ((hmq = WinCreateMsgQueue( hab, 0 )) == 0L)/* Create a msg queue */
  83.      AbortSS(hwndFrame, hwndClient); /* Terminate the application    */
  84.  
  85.   if (!WinRegisterClass(                /* Register window class        */
  86.      hab,                               /* Anchor block handle          */
  87.      (PSZ)"SSWindow",                   /* Window class name            */
  88.      (PFNWP)SSWindowProc,               /* Address of window procedure  */
  89.      CS_SIZEREDRAW,                     /* Class style                  */
  90.      0                                  /* No extra window words        */
  91.      ))
  92.      AbortSS(hwndFrame, hwndClient); /* Terminate the application    */
  93.  
  94.    flCreate = FCF_STANDARD &            /* Set frame control flags to   */
  95.              ~FCF_SHELLPOSITION &       /* standard except for shell    */
  96.              ~FCF_ACCELTABLE;           /* positioning.                 */
  97.  
  98.   if ((hwndFrame = WinCreateStdWindow(
  99.                HWND_DESKTOP,            /* Desktop window is parent     */
  100.                0,                       /* STD. window styles           */
  101.                &flCreate,               /* Frame control flag           */
  102.                "SSWindow",              /* Client window class name     */
  103.                "",                      /* Window text                  */
  104.                0,                       /* No special class style       */
  105.                (HMODULE)0L,             /* Resource is in .EXE file     */
  106.                ID_WINDOW,               /* Frame window identifier      */
  107.                &hwndClient              /* Client window handle         */
  108.                )) == 0L)
  109.      AbortSS(hwndFrame, hwndClient); /* Terminate the application    */
  110.  
  111.   WinSetWindowText(hwndFrame, "Swapper Size");
  112.  
  113.   if (!WinSetWindowPos( hwndFrame,      /* Shows and activates frame    */
  114.                    HWND_TOP,            /* window at position 100, 100, */
  115.                    100, 100, 180, 100,  /* and size 180, 100.           */
  116.                    SWP_SIZE | SWP_MOVE | SWP_ACTIVATE | SWP_SHOW
  117.                  ))
  118.      AbortSS(hwndFrame, hwndClient); /* Terminate the application    */
  119.  
  120. /************************************************************************/
  121. /* Get and dispatch messages from the application message queue         */
  122. /* until WinGetMsg returns FALSE, indicating a WM_QUIT message.         */
  123. /************************************************************************/
  124.  
  125.   while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  126.     WinDispatchMsg( hab, &qmsg );
  127.   WinDestroyWindow(hwndFrame);           /* Tidy up...                   */
  128.   WinDestroyMsgQueue( hmq );             /* Tidy up...                   */
  129.   WinTerminate( hab );                   /* Terminate the application    */
  130. } /* End of main */
  131.  
  132. /******************************************************************************/
  133. /*                                                                            */
  134. /*  SSWINDOWPROC is the window procedure which continuously monitors the size */
  135. /*  of the swap file and displays the size in the client area.                */
  136. /*                                                                            */
  137. /******************************************************************************/
  138. MRESULT EXPENTRY SSWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  139. {
  140.  
  141.   switch( msg )
  142.   {
  143.     case WM_CREATE:
  144.       swapsize = GetSwapperSize();
  145.  
  146.       oldswapsize = swapsize;
  147.       WinStartTimer(hab, hwnd, idTimer, timerinterval);
  148.       break;
  149.  
  150.     case WM_COMMAND:
  151.       {
  152.       USHORT command;                   /* WM_COMMAND command value     */
  153.       command = SHORT1FROMMP(mp1);      /* Extract the command value    */
  154.       switch (command)
  155.       {
  156.         case ID_10SECS:
  157.           timerinterval = 10 * 1000;
  158.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  159.                            ID_10SECS, TRUE);
  160.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  161.                            ID_30SECS, FALSE);
  162.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  163.                            ID_60SECS, FALSE);
  164.           WinStartTimer(hab, hwnd, idTimer, timerinterval);
  165.           break;
  166.         case ID_30SECS:
  167.           timerinterval = 30 * 1000;
  168.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  169.                            ID_10SECS, FALSE);
  170.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  171.                            ID_30SECS, TRUE);
  172.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  173.                            ID_60SECS, FALSE);
  174.           WinStartTimer(hab, hwnd, idTimer, timerinterval);
  175.           break;
  176.         case ID_60SECS:
  177.           timerinterval = 60 * 1000;
  178.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  179.                            ID_10SECS, FALSE);
  180.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  181.                            ID_30SECS, FALSE);
  182.           WinCheckMenuItem(WinWindowFromID(hwndFrame,FID_MENU),
  183.                            ID_60SECS, TRUE);
  184.           WinStartTimer(hab, hwnd, idTimer, timerinterval);
  185.           break;
  186.         default:
  187.           return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  188.       }
  189.       break;
  190.       }
  191.    case WM_TIMER:
  192.       swapsize = GetSwapperSize();
  193.       if (swapsize==0) {
  194.          WinMessageBox(HWND_DESKTOP,         /* Parent window is desk top */
  195.             hwndFrame,            /* Owner window is our frame */
  196.             "Cannot locate the Swapper File, Check your parameter.",
  197.             "Error Msg",          /* Title bar message         */
  198.             MSGBOXID,             /* Message identifier        */
  199.             MB_MOVEABLE | MB_CUACRITICAL | MB_CANCEL ); /* Flags */
  200.          WinPostMsg( hwnd, WM_CLOSE, (MPARAM)0,(MPARAM)0 );
  201.       }
  202.       if (swapsize != oldswapsize)
  203.       {
  204.          oldswapsize = swapsize;
  205.          WinInvalidateRect( hwnd, (PRECTL)NULL, FALSE);
  206.       }
  207.       break;
  208.     case WM_ERASEBACKGROUND:
  209.       /******************************************************************/
  210.       /* Return TRUE to request PM to paint the window background       */
  211.       /* in SYSCLR_WINDOW.                                              */
  212.       /******************************************************************/
  213.       return (MRESULT)( TRUE );
  214.     case WM_PAINT:
  215.       /******************************************************************/
  216.       /* Window contents are drawn here in WM_PAINT processing.         */
  217.       /******************************************************************/
  218.       {
  219.       HPS    hps;                       /* Presentation Space handle    */
  220.       RECTL  rc;                        /* Rectangle coordinates        */
  221.       POINTL pt;                        /* String screen coordinates    */
  222.                                         /* Create a presentation space  */
  223.       char   buffer[STRINGLENGTH];
  224.       char   temp[STRINGLENGTH];
  225.       int len;
  226.  
  227.       hps = WinBeginPaint( hwnd, 0L, &rc );
  228.       pt.x = 50; pt.y = 20;             /* Set the text coordinates,    */
  229.       GpiSetColor( hps, CLR_NEUTRAL );         /* colour of the text,   */
  230.       GpiSetBackColor( hps, CLR_BACKGROUND );  /* its background and    */
  231.       GpiSetBackMix( hps, BM_OVERPAINT );      /* how it mixes,         */
  232.                                                /* and draw the string...*/
  233.       WinFillRect( hps, &rc, CLR_BACKGROUND );
  234.       szString = _ltoa (swapsize, buffer, 10);
  235.       if ((len=strlen(buffer)) > 3) {
  236.          memcpy(temp, buffer, len+1);
  237.          memcpy(buffer, temp, len-3);
  238.          buffer[len-3] = ' ';
  239.          strcpy(&buffer[len-2], &temp[len-3]);
  240.          }
  241.  
  242.       strcat( szString, " KB");
  243.       GpiCharStringAt( hps, &pt, (LONG)strlen( szString ), szString );
  244.       WinEndPaint( hps );                      /* Drawing is complete   */
  245.       break;
  246.       }
  247.     case WM_CLOSE:
  248.       /******************************************************************/
  249.       /* This is the place to put your termination routines             */
  250.       /******************************************************************/
  251.       WinStopTimer( hab, hwnd, idTimer);
  252.       WinPostMsg( hwnd, WM_QUIT, (MPARAM)0,(MPARAM)0 );/* Cause termination*/
  253.       break;
  254.     default:
  255.       /******************************************************************/
  256.       /* Everything else comes here.  This call MUST exist              */
  257.       /* in your window procedure.                                      */
  258.       /******************************************************************/
  259.  
  260.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  261.   }
  262.   return (MRESULT)FALSE;
  263. } /* End of MyWindowProc */
  264.  
  265. /**************************************************************************/
  266. /* AbortSS    -- report an error returned from an API service.            */
  267. /*                                                                        */
  268. /* The error message is displayed using a message box and the program     */
  269. /* exit will be affected with the error status of 1.                      */
  270. /**************************************************************************/
  271. VOID AbortSS(HWND hwndFrame,HWND hwndClient)
  272. {
  273. PERRINFO  pErrInfoBlk;
  274. PSZ       pszOffSet;
  275. void      stdprint(void);
  276.  
  277.       DosBeep(100,10);
  278.       if ((pErrInfoBlk = WinGetErrorInfo(hab)) != (PERRINFO)NULL)
  279.       {
  280.       pszOffSet = ((PSZ)pErrInfoBlk) + pErrInfoBlk->offaoffszMsg;
  281.       pszErrMsg = ((PSZ)pErrInfoBlk) + *((PSHORT)pszOffSet);
  282.       if((INT)hwndFrame && (INT)hwndClient)
  283.          WinMessageBox(HWND_DESKTOP,         /* Parent window is desk top */
  284.                        hwndFrame,            /* Owner window is our frame */
  285.                        (PSZ)pszErrMsg,       /* PMWIN Error message       */
  286.                        "Error Msg",          /* Title bar message         */
  287.                        MSGBOXID,             /* Message identifier        */
  288.                        MB_MOVEABLE | MB_CUACRITICAL | MB_CANCEL ); /* Flags */
  289.       WinFreeErrorInfo(pErrInfoBlk);
  290.       }
  291.       WinPostMsg(hwndClient, WM_QUIT, (MPARAM)0, (MPARAM)0);
  292. } /* End of AbortHello */
  293. /* Function which returns swap file size in KB            */
  294. ULONG GetSwapperSize ()
  295. {
  296.    HDIR fhandle;
  297.    unsigned LONG count;
  298.    int fsize;
  299.    USHORT frc;
  300.    FILEFINDBUF buffer;   /* file information struct */
  301.  
  302.    count = 1;
  303.    fhandle = 0xFFFF;
  304.    frc = DosFindFirst (szFname, &fhandle, 0, &buffer, sizeof(buffer),                       &count, 1L);
  305.    if (frc != 0){
  306.       return(0L);
  307.    } /* endif */
  308.    fsize = buffer.cbFileAlloc / 1024;  /* in KBytes */
  309.    DosFindClose (fhandle);
  310.    return((ULONG)fsize);
  311. }
  312. /*********************** End of the hello.c *******************************/
  313.